package main.java.creatures;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import main.java.creatures.plant.SimplePlant;
import main.java.creatures.seed.ISeed;
import main.java.creatures.seed.Seed;
import creatures.ICreature;
import creatures.IEnvironment;

public class FactoryCreatures {
	
	public static final FactoryCreatures instance= new FactoryCreatures();
	
	private FactoryCreatures() {
	}
	
	
	@SuppressWarnings("rawtypes")
	private Class[] types = {IEnvironment.class, Point2D.class, double.class, double.class, Color.class};

	public void createHerbivorous(AbstractCreatureEcoSystem creature){
		Constructor<? extends ICreature> c;
		try {
			c = (Constructor<? extends ICreature>)
					creature.getClass().getDeclaredConstructor(types);
			c.setAccessible(true);
			creature.getEnvironment().addCreature(c.newInstance(creature.getEnvironment(),new Point2D.Double(creature.getPosition().getX()+1,creature.getPosition().getY()+1),creature.getDirection(),creature.getSpeed(),creature.getColor()));
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
	}
	
	public void createPlant(AbstractCreatureEcoSystem creature){
		
		Constructor<? extends ICreature> c;
		
		try {
			c = (Constructor<? extends ICreature>)
					creature.getSeed().getType().getDeclaredConstructor(types);
			c.setAccessible(true);
			creature.getEnvironment().addCreature(c.newInstance(creature.getEnvironment(),creature.getPosition(),creature.getDirection(),creature.getSpeed(),creature.getColor()));

		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		
	}

	public  BlockingQueue<ISeed> createsSeeds(int number, Class<? extends SimplePlant>type){
		if(number<=0){
			return new ArrayBlockingQueue<ISeed>(0);
		}
		BlockingQueue<ISeed> listSeed = new ArrayBlockingQueue<ISeed>(number);
		for(int i=0;i<number;i++){
			listSeed.add(new Seed(type));
		}
			return listSeed;
		
	}
}

